package cz.zcu.fav.rat.security.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import cz.zcu.fav.rat.beans.User;
import cz.zcu.fav.rat.security.beans.Role;
import cz.zcu.fav.rat.security.beans.UserDetailsBean;

@Service("defaultUserDetailsService")
public class DefaultUserDetailsService extends AbstractUserLoginMapper implements UserDetailsService
{
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException
	{
		User user = getUser(username);
		Role role = user.getRole();

		UserDetailsBean userDetails = new UserDetailsBean();
		List<String> roleList = new ArrayList<String>();
		roleList.add(role.toString());
		userDetails.setId(user.getId());
		userDetails.setAuthorities(roleList);
		userDetails.setUserName(username);
		userDetails.setPassword(user.getPasshash());
		userDetails.setImplementor(user.getImplementor());
		
		return userDetails;
	}

	@Override
	protected LoginImplementor getImplementor()
	{
		return new LoginImplementor("DEFAULT", true);
	}

}
